package com.qitianyong.sgkusbcamera;

import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;

import com.qitianyong.extendUI.ViewsCircle;

import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Rule
    public ActivityTestRule<PreviewActivity> mActivityTestRule = new ActivityTestRule<>(PreviewActivity.class);

    @Test
    public void resetFactory() throws Exception {
        onView(ViewMatchers.withId(R.id.views_circle)).perform(new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return isAssignableFrom(ViewsCircle.class);
            }
            @Override
            public String getDescription() {
                return "scroll to factory";
            }
            @Override
            public void perform(UiController uiController, View view) {
                ( view).scrollBy(500, 0);
            }
        });
        waitVisibility(false, R.id.iv_defaultbg);

        while (true) {
            //恢复出厂的容器非AdapterView，但用了Adapter 无法使用scrollTo
            onView(ViewMatchers.withText(R.string.text_defaultfactory)).perform(click());
            onView(ViewMatchers.withId(R.id.btn_positive)).perform(click());
            waitVisibility(true, R.id.iv_defaultbg);
            waitVisibility(false, R.id.iv_defaultbg);
        }
    }
    @Test
    public void sdFormat() throws Exception {
        waitVisibility(false, R.id.iv_defaultbg);
        int time = 0;
        while (true) {
            onView(ViewMatchers.withText(R.string.text_format)).perform(click());
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            onView(ViewMatchers.withId(R.id.btn_positive)).perform(click());
            waitToast(R.string.tip_format_ok);
            System.out.println(++time);
            try {
                Thread.sleep(3500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public void waitToast(@StringRes int stringResId){
        while (true){
            try {
                onView(withText(stringResId))
                        .inRoot(withDecorView(not(is(mActivityTestRule.getActivity().getWindow().getDecorView()))))
                        .check(matches(isDisplayed()));
                break;
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void waitVisibility(boolean visibility, @IdRes int id) {
        while (true) {
            if (isVisibility(id) == visibility) {
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean isVisibility(@IdRes int id) {
        final boolean[] vis = new boolean[1];
        onView(ViewMatchers.withId(id)).perform(new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return isAssignableFrom(View.class);
            }

            @Override
            public String getDescription() {
                return "get vis from View";
            }

            @Override
            public void perform(UiController uiController, View view) {
                vis[0] = view.getVisibility() == View.VISIBLE;
            }
        });
        return vis[0];
    }
}
